﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using MemberMgr.Data.BusinessObjects;
using MemberMgr.Data.Base;
using Ext.Net;
using MemberMgr.Data.ManagerObjects;

namespace MemberMgr.Website.Admin
{
    public partial class Products : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest && !this.IsPostBack)
            {

            }
        }

        [DirectMethod]
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strid = this.txtID.Text.Trim();
            string name = this.txtProductName.Text.Trim();
            string type = this.txtProductType.Text.Trim();
            string maker = this.txtMaker.Text.Trim();
            string price = this.txtPrice.Text.Trim();
            string comment = this.txtComment.Text.Trim();

            ItProductManager productMgr = Common.CommonUtil.managerFactory.GettProductManager();

            productMgr.Session.BeginTransaction();

            try
            {
                if (string.IsNullOrEmpty(strid))
                {
                    //Add

                    tProduct product = new tProduct();

                    product.Productname = name;
                    product.Producttype = type;
                    product.Maker = maker;
                    product.Comment = maker;
                    product.Price = uint.Parse(price);

                    productMgr.Save(product);
                }
                else
                {
                    //Update
                    uint id = uint.Parse(strid);

                    tProduct product = productMgr.GetById(id);

                    if (product != null)
                    {
                        product.Productname = name;
                        product.Producttype = type;
                        product.Maker = maker;
                        product.Comment = maker;
                        product.Price = uint.Parse(price);

                        productMgr.Update(product);
                    }

                }

                productMgr.Session.CommitTransaction();
            }
            catch (Exception ex)
            {
                Common.CommonUtil.logger.Error(ex.Message, ex);
                productMgr.Session.RollbackTransaction();
            }

            this.winDetail.Hide();

            BindDataSource();

        }

        [DirectMethod]
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindDataSource();
        }

        [DirectMethod]
        public void editProduct(uint productID)
        {
            ItProductManager productMgr = Common.CommonUtil.managerFactory.GettProductManager();

            tProduct product = productMgr.GetById(productID);

            if (product != null)
            {
                this.panelDetail.SetValues(new
                {
                    product.Id,
                    product.Maker,
                    product.Comment,
                    product.Price,
                    product.Productname,
                    product.Producttype
                });

                this.winDetail.Show();
            }
        }

        [DirectMethod]
        protected void storeProduct_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            this.BindDataSource();
        }

        [DirectMethod(ShowMask=true,Msg="数据删除中...")]
        public void deleteProduct(uint productID)
        {
            if (productID != 0)
            {
                ItProductManager productMgr = Common.CommonUtil.managerFactory.GettProductManager();

                productMgr.Session.BeginTransaction();

                try
                {
                    tProduct product = productMgr.GetById(productID);

                    if (product != null)
                    {
                        productMgr.Delete(product);
                    }

                    productMgr.Session.CommitTransaction();
                }
                catch (Exception ex)
                {
                    Common.CommonUtil.logger.Error(ex.Message, ex);

                    productMgr.Session.RollbackTransaction();
                }

                this.BindDataSource();

            }
        }

        private void BindDataSource()
        {
            string productName = this.txtSearchName.Text;

            ItProductManager productMgr = Common.CommonUtil.managerFactory.GettProductManager();
            IList<tProduct> list = null;

            if (!string.IsNullOrEmpty(productName))
            {
                list = productMgr.GetProductsByName(productName);
            }
            else
            {
                list = productMgr.GetAll();
            }

            if (list != null)
            {
                DataTable dt = this.GetMemberInfoDataTable();

                foreach (tProduct item in list)
                {
                    decimal productSUM = 0;

                    productSUM += item.TProductInlogs.Sum<tProductInlog>(p => p.Insum);
                    productSUM = productSUM - item.TProductOutlogs.Sum<tProductOutlog>(p => p.Outsum);

                    dt.Rows.Add(
                        item.Id,
                        item.Productname,
                        item.Producttype,
                        item.Maker,
                        item.Price,
                        item.Comment,
                        productSUM
                        );
                }

                this.storeProduct.DataSource = dt;
                this.storeProduct.DataBind();
            }
        }

        private DataTable GetMemberInfoDataTable()
        {
            DataTable dt = new DataTable();

            dt.Columns.AddRange(new DataColumn[] { 
                new DataColumn() { ColumnName="ID",DataType=typeof(uint)},
                new DataColumn() { ColumnName="PRODUCTNAME",DataType=typeof(string)},
                new DataColumn() { ColumnName="PRODUCTTYPE",DataType=typeof(string)},
                new DataColumn() { ColumnName="MAKER",DataType=typeof(string)},
                new DataColumn() { ColumnName="PRICE",DataType=typeof(double)},
                new DataColumn() { ColumnName="COMMENT",DataType=typeof(string)},
                new DataColumn() { ColumnName="PRODUCTSUM",DataType=typeof(decimal)}
            });

            return dt;
        }

    }
}
